/*
 * @Author: tkp
 * @Date: 2021-11-20 16:40:50
 */
/**
 * 
 * @param {*} s 
 * @param {*} f 
 * @param {*} e 
 * @returns 
 */
export function myReplace(s, f, e) { //吧f替换成e
    var reg = new RegExp(f, "g"); //创建正则RegExp对象   
    return s.replace(reg, e);
}
/**
 * 
 * @param {*} s 
 * @param {*} len 
 * @returns 
 */
//清除文本中特殊字符 表情符号 空格
export function delOther(s, len) {
    if (s.length > 0) {
        s = s.replace(/<\/?[^>]*，>/g, '')
        var pattern = new RegExp("[`~!@#$^&*()=|{}''\\[\\].<>/~@#￥……&*（）{}【】‘”“']")
        var rs = ""
        for (var i = 0; i < s.length; i++) {
            rs = rs + s.substr(i, 1).replace(pattern, '')
        }
        s = rs
        s = s.replace(
            /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig,
            "").replace(/\s+/g, '')
    }
    return s
}
/**
 * 
 * @param {*} code 
 * @returns 
 */
//验证手机号格式
export function checkmobile(code) {
    var myreg =
        /^(((13[0-9]{1})|(15[0-9]{1})|(14[0-9]{1})|(16[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\d{8})$/; //手机正则式
    if (!myreg.test(code))
        return false
    else
        return true
}
/**
 * 
 * @param {*} s 
 * @returns 
 */
//验证正整数
export function isIntegers(s) {
    let check = /^[0-9]\d*$/
    if (check.test(s)) {
        return true
    } else {
        return false
    }
}
/**
 * 获取日期时间
 * //时间戳
 * @param {*} timestamp   
 */
export function getDateTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
}
/**
 * 
 * @param {*} timestamp 
 * @returns 
 */
export function getDate(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    return Y + M + D;
}
/**
 * 
 * @param {*} timestamp 
 * @returns 
 */
export function getDatePart(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    return { Y, M, D };
}
/**
 * 
 * @param {*} date 
 * @returns 
 */
//格式化条件查询中的时间
export function addDateRange(date) {
    let param = { begin: '', end: '' }
    if (date != '' && typeof (date) != undefined && date != null) {
        param.begin = date[0] + ' ' + '00:00:00'
        param.end = date[1] + ' ' + '23:59:59'
    }
    return param
}
export function addDateRange2(date) {
    let param = { begin: '', end: '' }
    if (date != '' && typeof (date) != undefined && date != null) {
        param.begin = date[0]
        param.end = date[1]
    }
    return param
}
/**
 * confirm弹窗
 * @param {标题} title 
 * @param {副标题} subTitle 
 * @param {回调} confirm 
 */
export function confirmPop(title, subTitle, confirm) {
    this.$confirm(title, subTitle, {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
    }).then(result => {
        if (result == 'confirm') { return confirm() } else { return }
    }).catch((e) => { })
}
/**
 * 
 * @param {提示成功信息} message 
 */
export function messageSuccPop(message) {
    this.$message({ type: 'success', message: message })
}
/**
 * 
 * @param {提示警告信息} message 
 */
export function messageWarnPop(message) {
    this.$message({ type: 'warning', message: message })
}
/**
 * 
 * @param {提示错误信息} message 
 */
export function messageErrorPop(message) {
    this.$message({ type: 'error', message: message })
}
/**
 * @param {*} data 
 * @returns 
 */
export function getJsonParse(data) {
    return JSON.parse(data)
}
/**
 * 
 * @param {*} data 
 * @returns 
 */
export function getJsonStringify(data) {
    return JSON.stringify(data)
}
export function copyObjectKey(sourceObj, targetObj, filedKey) {
    for (const key in sourceObj) {
        if (sourceObj.hasOwnProperty(key)) {
            targetObj[key] = ''; // 复制key并将值设置为空
        }
    }
    return targetObj
}
/**
 * 
 * @param {*} arr  目标数组
 * @param {*} targetId 目标对象内的id
 * @returns 
 */
export function findObjectWithId(arr, targetField, targetId) {
    for (let item of arr) {
        if (typeof item === 'object') {
            if (item[targetField] === targetId) {
                return item;
            } else if (Array.isArray(item.children)) {
                let foundObject = findObjectWithId(item.children, targetField, targetId);
                if (foundObject) {
                    return foundObject;
                }
            }
        }
    }
    return null;
}
/**
 * 整理侧边栏菜单
 * @param {*} data  目标数组
 */
export function getMenu(data) {
    let navMenu = []
    if (data) {
        data.forEach(item => {
            if (item.buttonFlag == 0 && item.visible == 1 && item.enable == 1 && item.level == 1) {
                let param = {
                    menuId: item.menuId,
                    menuName: item.menuName,
                    parentsId: item.parentsId,
                    moduleId: item.moduleId,
                    frontRoute: item.frontRoute,
                    iconUrl: item.iconUrl,
                    children: [],
                }
                if (item.children) {
                    item.children.forEach(it => {
                        if (it.buttonFlag == 0 && it.visible == 1 && it.enable == 1 && it.level == 2) {
                            let menuItem = {
                                parentsId: it.parentsId,
                                moduleId: it.moduleId,
                                menuName: it.menuName,
                                menuId: it.menuId,
                                iconUrl: it.iconUrl,
                                frontRoute: it.frontRoute,
                                iconUrl: item.iconUrl,
                            }
                            param.children.push(menuItem)
                        }
                    })
                }
                navMenu.push(param)
            }
        })
    }
    return { navMenu }
}
/**
 * 整理动态路由
 * @param {*} data 目标数组
 */
export function filterAsyncPermission(arrString) {
    let routeArr = []
    let data = JSON.parse(arrString)
    if (data) {
        data.forEach(item => {
            if (item.buttonFlag == 0 && item.visible == 1 && item.enable == 1 && item.level == 1) {
                let param = {
                    frontPath: item.frontPath,
                    menuName: item.menuName,
                    menuId: item.menuId,
                    frontRoute: item.frontRoute,
                    parentsId: item.parentsId,
                    moduleId: item.moduleId,
                    iconUrl: item.iconUrl,
                    meta: {
                        permission: [],
                        title: item.menuName,
                        icon: item.iconUrl,
                    },
                    children: [],
                }
                if (item.children && item.children.length > 0) {
                    item.children.forEach(it => {
                        if (it.buttonFlag == 1 && it.enable == 1 && it.visible === 1) {//是权限按钮
                            let metaParam = {
                                menuName: it.menuName,
                                menuId: it.menuId,
                                frontRoute: it.frontRoute,
                                parentsId: it.parentsId
                            }
                            param.meta.permission.push(metaParam)
                        } else if (it.buttonFlag == 0 && it.enable == 1 && it.visible === 1) {//是菜单
                            let menuItem = {
                                frontPath: it.frontPath,
                                menuName: it.menuName,
                                menuId: it.menuId,
                                frontRoute: it.frontRoute,
                                parentsId: it.parentsId,
                                moduleId: it.moduleId,
                                iconUrl: it.iconUrl,
                                meta: {
                                    permission: [],
                                    title: it.menuName,
                                    icon: it.iconUrl,
                                },
                                children: [],
                            }
                            if (it.children && it.children.length > 0) {
                                it.children.forEach(i => {
                                    if (i.buttonFlag == 1 && i.enable == 1 && i.visible === 1) {
                                        let metaParam = {
                                            menuName: i.menuName,
                                            menuId: i.menuId,
                                            frontRoute: i.frontRoute,
                                            parentsId: i.parentsId
                                        }
                                        menuItem.meta.permission.push(metaParam)
                                    }
                                })
                            }
                            param.children.push(menuItem)
                        }
                    })
                }
                routeArr.push(param)
            }
        })
    }
    return routeArr || []
}